//
//  SearchHDController.m
//  HiCar
//
//  Created by 丁一 on 2019/4/26.
//  Copyright © 2019 丁一. All rights reserved.
//

#import "SearchHDController.h"
#import "SearchHDCell.h"
#import "HomeNewsCell.h"
#import "NewsModel.h"
#import "NewsAction.h"
#import "SearchHDVModel.h"
#import "JoneCunChu.h"
#import "MementoCenter.h"
#import "NSObject+MementoCenter.h"

@interface SearchHDController ()<UISearchBarDelegate,UIViewControllerPreviewingDelegate,SearchHDDelegate>{
    NSMutableArray *SearAraay;
    BOOL            Search_ok;
    NSInteger       page;
}

@property (nonatomic, strong) UISearchBar      *TsearchBar;

@end

@implementation SearchHDController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.titleView=self.TsearchBar;
    
    self.Data_array = [NSMutableArray new];
    SearAraay = [NSMutableArray new];
    JoneCunChu *shuju=[[JoneCunChu alloc] init];
    if ([[NSUserDefaults standardUserDefaults] objectForKey:@"SearchArray"]) {
        [shuju recoverFormStateWithKey:@"SearchArray"];
        SearAraay = shuju.JoneArray;
    }
    
    [self.view addSubview:self.TableView];
    self.TableView.frame=CGRectMake(0, NAV_Height+10, RECRWITCH, RECERHEIGHT - (NAV_Height+10));
    MJHiCarHeader *header           = [MJHiCarHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadnew)];
    header.automaticallyChangeAlpha    = YES;
    header.lastUpdatedTimeLabel.hidden = YES;
    header.stateLabel.hidden           = YES;
    self.TableView.mj_header          = header;
    self.TableView.mj_header.automaticallyChangeAlpha = YES;
    self.TableView.mj_footer           = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        [self laodmore];
    }];
    self.TableView.mj_footer.hidden    = YES;
    
    [_TsearchBar becomeFirstResponder];
    Search_ok = YES;
    
    [self setnavback];
}

- (void)loadnew{
    page = 0;
    self.TableView.mj_footer.hidden    = YES;
    [self laod_net_dic];
}

- (void)laodmore{
    NewsModel *modellin = [self.Data_array lastObject];
    page = [modellin.news_id integerValue];
    [self laod_net_dic];
}

- (void)laod_net_dic{
    NSMutableDictionary *postdic = [NSMutableDictionary new];
    [postdic setValue:[NSString stringWithFormat:@"%li",(long)page] forKey:@"page"];
    [postdic setValue:_TsearchBar.text forKey:@"key"];
    SearchHDVModel *viewmodel = [[SearchHDVModel alloc] init:postdic];
    viewmodel.delegate = self;
}

- (void)SearchHDSucces:(int)value array:(NSMutableArray *)array page:(NSInteger)page{
    if (value == 1) {
        if (page==0) {
            [self.Data_array removeAllObjects];
        }
        [self.Data_array addObjectsFromArray:array];
        
        if (array.count<15) {
            self.TableView.mj_footer.hidden = YES;
        }else{
            self.TableView.mj_footer.hidden = NO;
        }
    }
    [self.TableView.mj_header endRefreshing];
    [self.TableView.mj_footer endRefreshing];
    [self.TableView reloadData];
}

-(UISearchBar *)TsearchBar{
    if (!_TsearchBar) {
        _TsearchBar                 = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, RECRWITCH, 40)];
        _TsearchBar.placeholder     = @"输入搜索内容";
        _TsearchBar.barTintColor    = [JonesClass stringTOColor:@"#f5f5f5"];
        _TsearchBar.delegate        = self;
        _TsearchBar.searchBarStyle  = UISearchBarStyleMinimal;
        _TsearchBar.backgroundColor = [UIColor clearColor];
    }
    return _TsearchBar;
}

-(BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar{
    return YES;
}

- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar {
    [searchBar setShowsCancelButton:NO animated:YES];
    
    self.TableView.mj_footer.hidden    = YES;
    self.TableView.mj_header.hidden    = YES;
}

#pragma mark - UISearchBarDelegate
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    if (searchBar.text!=nil) {
        BOOL  linshibool = NO;
        
        for (NSString *linst in SearAraay) {
            if ([linst isEqualToString:searchBar.text]) {
                linshibool =YES;
            }
        }
        
        if (linshibool) {
            [SearAraay removeObject:searchBar.text];
        }
        [SearAraay insertObject:searchBar.text atIndex:0];
        
        JoneCunChu *shuju=[[JoneCunChu alloc] init];
        shuju.JoneArray=SearAraay;
        [shuju saveStateWithKey:@"SearchArray"];
        
        
        Search_ok = NO;
        [self.TableView reloadData];
        self.TableView.mj_header.hidden = NO;
        [self.TableView.mj_header beginRefreshing];
        [_TsearchBar resignFirstResponder];
    }
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    if (Search_ok) {
        return YES;
    }else{
        return NO;
    }
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        [SearAraay removeObjectAtIndex:indexPath.row];
        
        JoneCunChu *shuju = [[JoneCunChu alloc] init];
        shuju.JoneArray   = SearAraay;
        [shuju saveStateWithKey:@"SearchArray"];
        
        if (SearAraay.count == 0) {
            [self.TableView reloadData];
        }else{
            [self.TableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
        }
    }
    else if (editingStyle == UITableViewCellEditingStyleInsert) {}
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (Search_ok) {
        return SearAraay.count;
    }
    return self.Data_array.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (Search_ok) {
        SearchHDCell *cell = [SearchHDCell cellForTableView:tableView cellForRowAtIndexPath:indexPath];
        cell.label_here.text=[NSString stringWithFormat:@"%@",[SearAraay objectAtIndex:indexPath.row]];
        return cell;
    }else{
        HomeNewsCell *cell = [HomeNewsCell cellForTableView:tableView cellForRowAtIndexPath:indexPath];
        cell.Model = [self.Data_array objectAtIndex:indexPath.row];
        return cell;
    }
}

-(void)gunagaogoto{
    [SearAraay removeAllObjects];
    JoneCunChu *shujus=[[JoneCunChu alloc] init];
    shujus.JoneArray=SearAraay;
    [shujus saveStateWithKey:@"SearchArray"];
    [self.TableView reloadData];
}

#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (Search_ok) {
        [_TsearchBar resignFirstResponder];
        _TsearchBar.text = [SearAraay objectAtIndex:indexPath.row];
        Search_ok = NO;
        [self.TableView reloadData];
        self.TableView.mj_header.hidden = NO;
        [self.TableView.mj_header beginRefreshing];
    }else{
        NewsModel *model = [self.Data_array objectAtIndex:indexPath.row];
        [NewsAction Actiontodone:JonesGOTO_NewsInfo view:self dictionary:@{@"model":model}];
        model.IsRead = YES;
        [tableView reloadData];
        [tableView deselectRowAtIndexPath:indexPath animated:YES];
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (Search_ok) {
        return 45;
    }else{
        NewsModel *model = [self.Data_array objectAtIndex:indexPath.row];
        return model.news_height;
    }
}

@end
